<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class PublicController extends BaseController {

    public function publicadd(){

		$d = D(C('TABLE_PUBLICTYPE'));
    	$result = $d->order('p_sn asc')->select();
    	$this->assign('result',$result);
    	$this->display();
    }
    
    public function add(){
    	
    	$link = I('post.p_link');
    	$img = I('post.p_img');
    	$sn = I('post.p_sn');
    	$xok = I('post.xok');
    	for($i=0;$i<count($sn);$i++){
    		$res = $this->insertdata($link[$i], $img[$i], $sn[$i],$xok);
    		if($res || $res==0){
    			if(I('post.xok')==1){
    				switch ($i){
    					case 0:
    						//echo $img[$i];
    						$this->replre(I('post.fzh'),$link[$i],$img[$i],3);
    						break;
    					case 1:
    						//echo $img[$i];
    						$this->replre(I('post.fzh'),$link[$i],$img[$i],4);
    						break;
    					case 2:
    						//echo $img[$i];
    						$this->replre(I('post.fzh'),$link[$i],$img[$i],2);
    						break;
    				}
    			}
    		}else{
    			$this->error('错误！','',1);
    		}
    	}
    	$this->success('执行成功！','',1);

    }
    
    /*
     * 获取某个账号下所做码的ID
     */
    protected function replre($userid,$jumpaddres,$thumbimg,$msort){
    	//echo $userid.$jumpaddres.$thumbimg.$msort;die;
   		$d = D(C('TABLE_TYPE'));
    	$res = $d->field('id')->where(array('userid'=>$userid))->select();
    	//dump($res);die;
    	if(!empty($res)){
    		foreach ($res as $key=>$v){
    			//echo $v['id'];
    			$this->getsubtypecontent($v['id'],$jumpaddres,$thumbimg,$msort);
    		}
    	}
    	
    }
    protected function getsubtypecontent($mtypeid,$jumpaddres,$thumbimg,$msort){
    	//echo $mtypeid.$jumpaddres.$thumbimg.$msort;die;
    	$d = D(C('TABLE_SUBMENU'));
    	$res = $d->where(array('mtypeid'=>$mtypeid,'msort'=>$msort))->select();
    	//dump($res);die;
    	if(!empty($res)){
    		foreach ($res as $key=>$v){
    			if($v['mtypeid']==$mtypeid and $v['mparent']==0){
    				$this->getsubtype($mtypeid,$jumpaddres,$thumbimg,$msort);
    			}
    			
    		}
    	}
    	 
    }
    /*
     * 批量替换
     */
    protected function getsubtype($mtypeid,$jumpaddres,$thumbimg,$msort){
    	//echo $mtypeid.$jumpaddres.$thumbimg.$msort."<br>";
    	$d1 = D(C('TABLE_SUBMENU'));
    	$d1->jumpaddres = $jumpaddres;
    	$d1->thumbimg = $thumbimg;
    	$result1 = $d1->where(array('msort'=>$msort,'mtypeid'=>$mtypeid,'mparent'=>0))->save();
     	//echo $result1."<br>";
    	if($result){
    		return true;
    	}else{
    		return false;
    	}

    
    }
    
    protected function insertdata($link,$img,$sn,$xok){
    	$d = D(C('TABLE_PUBLICTYPE'));
    	$d->p_link = html_entity_decode($link);
    	$d->p_img = html_entity_decode($img);
    	$d->isc = $xok;
    	$result = $d->where(array('p_sn'=>$sn))->save();
    	return $result;

    }
    
    public function publicedit(){
    	$d = D(C('TABLE_PUBLICTYPE'));
    	$result = $d->order('p_sn asc')->select();
    	$this->assign('result',$result);
    	$this->display('publicadd');
    }

    


}